﻿using System;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using WI.CandidateManager.Common.Logging;

namespace CandidateManager
{
    public class MvcApplication : System.Web.HttpApplication
    {
        private readonly ILogger logger = LoggerManager.CreateLogger(typeof(MvcApplication));

        /// <summary>
        /// Application_s the start.
        /// </summary>
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            logger.Info("Candidate Manager Application start working");

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }

        /// <summary>
        /// Application_s the end.
        /// </summary>
        protected void Application_End()
        {
            logger.Info("Candidate Manager Application end working");
        }

        /// <summary>
        /// Application_s the error.
        /// </summary>
        protected void Application_Error()
        {
            string action = "General";
            Exception exception = Server.GetLastError();
            logger.Error("Technical error", exception);

            Response.Clear();

            HttpException httpException = exception as HttpException;

            if (httpException != null)
            {

                switch (httpException.GetHttpCode())
                {
                    case 404:
                        // page not found
                        action = "HttpError404";
                        break;
                    case 500:
                        // server error
                        action = "HttpError500";
                        break;
                }

                // clear error on server
                Server.ClearError();

            }
            Response.Redirect(String.Format("~/Error/{0}", action));
        }

        /// <summary>
        /// Handles the Start event of the Session control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        void Session_Start(object sender, EventArgs e)
        {
            //TODO: add user info from session
            logger.Info(String.Format("Session Started. SessionId: {0}", HttpContext.Current.Session.SessionID));
        }

        /// <summary>
        /// Handles the End event of the Session control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        //void Session_End(object sender, EventArgs e)
        //{
        //    //TODO: add user info from session
        //    logger.Info(String.Format("Session Ended. SessionId: {0}", HttpContext.Current.Session.SessionID));
        //}
    }
}